package com.luyun.frogpay.base.network.request;

import com.luyun.frogpay.base.network.request.progress.ProgressBean;
import com.luyun.frogpay.base.network.request.progress.ProgressHandler;
import com.luyun.frogpay.base.network.request.progress.ProgressListener;
import com.luyun.frogpay.base.network.request.progress.ProgressResponseBody;
import com.luyun.frogpay.base.utils.Log;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by 赵星宇 on 2018/1/26.
 * 拦截器工具类!
 */

public class InterceptorUtil {
    private static ProgressHandler mProgressHandler;
    private static ProgressBean progressBean = new ProgressBean();
    private static final Charset UTF8 = Charset.forName("UTF-8");

    //日志拦截器
    public static HttpLoggingInterceptor LogInterceptor() {
        return new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (message.startsWith("{") || message.startsWith("[")) {
                    Log.json(message);
                } else {
                    Log.d(message);
                }
            }
        }).setLevel(HttpLoggingInterceptor.Level.BODY);//设置打印数据的级别
    }

    public static Interceptor HeaderInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                //在这里可以做一些想做的事,比如token失效时,重新获取token
                //或者添加header等等
                Request request;
                Request.Builder builder = chain.request().newBuilder();
                request = builder.build();
                return chain.proceed(request);
            }
        };
    }

//    public static Interceptor ResponseInterceptor(){
//        return new Interceptor() {
//            @Override
//            public Response intercept(Chain chain) throws IOException {
//                Request request = chain.request().newBuilder().build();
//                chain.proceed(request).code()
//                return null;
//            }
//        };
//    }

    /**
     * 添加进度
     *
     * @return
     */
    public static Interceptor addProgress() {
        final ProgressListener progressListener = new ProgressListener() {
            //该方法在子线程中运行
            @Override
            public void onProgress(long progress, long total, boolean done) {
                if (mProgressHandler == null) {
                    return;
                }
                progressBean.setBytesRead(progress);
                progressBean.setContentLength(total);
                progressBean.setDone(done);
                mProgressHandler.sendMessage(progressBean);
            }
        };
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(
                        new ProgressResponseBody(originalResponse.body(), progressListener))
                        .build();
            }
        };
    }

    public static void setProgressHandler(ProgressHandler progressHandler) {
        mProgressHandler = progressHandler;
    }
}
